package definite.Guide.Book.JFileChooser;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class JFileChooser_InFrame {

	public static void main(String[] args) {
		Runnable r = new Runnable() {

			@Override
			public void run() {
				JFrame.setDefaultLookAndFeelDecorated(true);
				JFrame frame = new JFrame("Simple JFileChooser");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

				final JLabel dirLabel = new JLabel(" ");
				dirLabel.setFont(new Font("San-Serif", Font.BOLD | Font.ITALIC,
						18));
				frame.add(dirLabel, BorderLayout.NORTH);

				final JLabel fileLabel = new JLabel(" ");
				fileLabel.setFont(new Font("San-Serif",
						Font.BOLD | Font.ITALIC, 18));
				frame.add(fileLabel, BorderLayout.SOUTH);

				/*
				 * After creating a JFileChooserfrom a constructor,
				 *  you can place it in any Container, because it’s 
				aJComponent.
				 */

				// define actionListener for JFileChooser
				ActionListener action = new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						JFileChooser fChoose = (JFileChooser) e.getSource();
						String actionCMD = e.getActionCommand();
						if (actionCMD.equals(JFileChooser.APPROVE_SELECTION)) {
							File file = fChoose.getSelectedFile();
							fileLabel.setText(file.getName());
							dirLabel.setText(file.getParent());
						} else if (actionCMD
								.equals(JFileChooser.CANCEL_SELECTION)) {
							dirLabel.setText(" ");
							fileLabel.setText(" ");
						}
					}
				};

				JFileChooser fileChoose = new JFileChooser(".");
				fileChoose.setControlButtonsAreShown(false);
				fileChoose.addActionListener(action);
				frame.add(fileChoose, BorderLayout.CENTER);

				frame.pack();
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
			}
		};

		EventQueue.invokeLater(r);
	}

}
